package com.ym.b2b.provider.service.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.ym.b2b.commons.exception.BaseException;
import com.ym.b2b.commons.service.BaseService;
import com.ym.b2b.provider.dao.EmBProviderProductDAO;
import com.ym.b2b.provider.service.EmBProviderProductService;
import com.ym.b2b.provider.sqlbean.EmBProviderProduct;
import com.ym.b2b.user.dao.EmCUserDAO;
import com.zc.commons.base.Page;
import com.zc.commons.base.Result;

/**
 * @ClassName: EmBProviderProductServiceImpl
 * @Author tangxj
 * @Date 2015-3-20 上午11:19:09
 * @CopyRight B2B小组
 */

@Service
public class EmBProviderProductServiceImpl extends BaseService implements EmBProviderProductService {

    private Logger                logger = Logger.getLogger(getClass());
    @Autowired
    private EmBProviderProductDAO EmBProviderProductDAO;

    @Autowired
    private EmCUserDAO            userdao;
    
    private Logger         dbLog = Logger.getLogger("db");

    /*
     * 分页查询
     */
    @Override
    public Result searchPageEmBProviderProduct(Page<EmBProviderProduct> page, EmBProviderProduct EmBProviderProduct)
                                                                                                                    throws DataAccessException {
        Result result = new Result();

        try {
            result.setResultPage(EmBProviderProductDAO.searchPageEmBProviderProduct(EmBProviderProduct, page));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseException(Result.CODE_100, "查询异常");
        }
        return result;
    }

    /*
     * 插入
     */
    @Override
    public Result insertProviderProduct(EmBProviderProduct emBProviderProduct) throws DataAccessException {
        Result result = checkInputEmBProviderProduct(emBProviderProduct);
        if (!result.getCode().equals("200")) return result;
        try {
            if (!"".equals(emBProviderProduct.getProductCodeList())) {
                String[] productCodes = emBProviderProduct.getProductCodeList().split(",");
                for (String productCode : productCodes) {
                	emBProviderProduct.setProductCode(productCode);
                    EmBProviderProductDAO.insertEmBProviderProduct(emBProviderProduct);
                }
                dbLog.info("INSERT-->EM_B_PROVIDER_PRODUCT 供货商("+emBProviderProduct.getProviderCode()+")添加商品 " +"  商品编码："+emBProviderProduct.getProductCodeList());
            }
            result.setResult("供应商设置商品成功!");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseException(Result.CODE_100, "供应商设置商品失败！");

        }
        return result;
    }

    /*
     * 删除
     */
    @Override
    public Result deleteEmBProviderProduct(EmBProviderProduct emBProviderProduct) throws DataAccessException {
        Result result = new Result();
        try {
            EmBProviderProductDAO.deleteEmBProviderProduct(emBProviderProduct);
            dbLog.info("DELETE-->EM_B_PROVIDER_PRODUCT 供货商("+emBProviderProduct.getProviderCode()+")删除商品 " +"  数据值："+JSON.toJSONString(emBProviderProduct));
            result.setResult("删除供应商品成功！");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseException(Result.CODE_100, "删除供应商商品失败！");
        }

        return result;
    }

    @Override
    public Result deleteMultipleEmBProviderProducts(String providerProductIdList) {
        Result result = new Result();
        try {
            EmBProviderProductDAO.deleteMultipleEmBProviderProducts(providerProductIdList);
            dbLog.info("DELETE(Multiple-删除)-->EM_B_PROVIDER_PRODUCT "+"ID: "+providerProductIdList);
            result.setResult("批量删除供应商商品成功！");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseException(Result.CODE_100, "批量删除供应商商品失败！");
        }

        return result;
    }

    /**
     * 输入参数检查
     * 
     * @Title: checkInputEmCProvider
     */
    private Result checkInputEmBProviderProduct(EmBProviderProduct p) {
        Result result = new Result();
        if (StringUtils.isBlank(p.getProviderCode())) {
            result.setCodeResult(Result.CODE_100, "缺少providerCode供应商编码!");
            return result;
        }
        if (StringUtils.isBlank(p.getProductCodeList())) {
            result.setCodeResult(Result.CODE_100, "缺少ProductCode采购商编码!");
            return result;
        }
        return result;

    }

    /**
     * 根据providerCode查询所属产品list
     * <p>Title: getProviderProductList</p>
     */
	@Override
	public List<EmBProviderProduct> getProviderProductList(String providerCode) throws DataAccessException {
		return EmBProviderProductDAO.getProviderProductList(providerCode);
	}
}
